OpenBuildings GenerativeComponents Help

Specifying a Minimum Required Rank

In some contexts, we want to be able to specify that a value must be of a certain minimum rank. For example, suppose we're writing a function that processes each item in a list. Obviously, the value that we're working with must be a list in the first place. Whenever we define a new variable, we can specify the minimum accepted rank of any values assigned to that variable. To do so, we follow the data type name with a pair of empty square brackets for each rank.

For example…

  • int x — Defines a new variable named x, specifying that every value assigned to x must be a single integer, or a list of integers of any rank.
  • int[] x — Defines a new variable named x, specifying that every value assigned to x must be a list of integers of at least rank 1. (It will not accept a single integer.)
  • int[][] x — Defines a new variable named x, specifying that every value assigned to x must be a list of integers of at least rank 2. (It will not accept a single integer, nor a list of only rank 1.)
  • int[][][] x — (And so on.)

When a variable is defined with a certain number of empty square brackets, we know we can always index into that variable with at least the same number of square brackets. Suppose we define a variable using this form:

type [][][] variable

Since this definition specifies three levels of empty square brackets (i.e., rank 3), we know we can always index into that variable using three levels of indexing:

variable[a][b][c]

where a, b, and c are any integer expressions.

Example

bool[] successes;
successes = {true, false, true};                         
 // This works.
successes = {{true, false}, {false, true}, {true, true}}  
// And this works, too.
successes = true                                         
 // But this does not work!

The last statement produces the error message, 'Type mismatch error. A list is expected, but the given value is not a list.'